package fjut.acm.coding.controller;

import fjut.acm.coding.entity.DiscussPostPO;
import fjut.acm.coding.entity.result.ResultCode;
import fjut.acm.coding.entity.result.ResultJson;
import fjut.acm.coding.service.DiscussPostService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/discuss")
public class DiscussController {
    @Resource
    DiscussPostService discussPostService;

    @GetMapping("/post/list")
    public ResultJson getPostList(@RequestParam("pageNum") Integer pageNum,
                                  @RequestParam("pageSize") Integer pageSize) {
        ResultJson resultJson = new ResultJson();
        List<DiscussPostPO> discussPostPOS = discussPostService.pages(pageNum, pageSize);
        if (discussPostPOS.size() != 0) {
            resultJson.setStatus(ResultCode.REQUIRED_SUCCESS);
            resultJson.addInfo(discussPostPOS);
            resultJson.addInfo(discussPostPOS.size());
        } else {
            resultJson.setStatus(ResultCode.RESOURCE_NOT_EXIST);
        }
        return resultJson;
    }

    /**
    * @Description: 根据标题或者发帖人的昵称查找帖子
    * @Param:  content(标题或者发帖人昵称)
    * @return: team.fjut.cf.pojo.vo.ResultJson
    * @Author: liweihao
    * @Date: 2020/11/22
    */
    @GetMapping("/getDiscussByTitleOrNickName")
    public ResultJson getDiscussByTitleOrNickName(@RequestParam("content") String content,
                                                  @RequestParam("pageNum") Integer pageNum,
                                                  @RequestParam("pageSize") Integer pageSize) {
        ResultJson resultJson = new ResultJson();
        List<DiscussPostPO> discussPostPOS = discussPostService.getDiscussByTitleOrNickName(pageNum, pageSize, content);
        if (discussPostPOS.size() != 0) {
            resultJson.setStatus(ResultCode.REQUIRED_SUCCESS);
            resultJson.addInfo(discussPostPOS);
            resultJson.addInfo(discussPostPOS.size());
        } else {
            resultJson.setStatus(ResultCode.RESOURCE_NOT_EXIST);
        }
        return resultJson;
    }

    @PostMapping("/putDiscuss")
    public ResultJson putDiscuss(@RequestParam("title") String title,
                                 @RequestParam("author") String author) {
        ResultJson resultJson = new ResultJson(ResultCode.REQUIRED_SUCCESS);
        DiscussPostPO discussPostPO = new DiscussPostPO();
        discussPostPO.setTitle(title);
        discussPostPO.setTime(new Date());
        discussPostPO.setAuthor(author);
        discussPostPO.setPriority(5.0);
        int result  = discussPostService.putDiscuss(discussPostPO);
        if (result == 0) {
            resultJson.setStatus(ResultCode.BUSINESS_FAIL);
        }
        return resultJson;
    }
}
